using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using LocalBoard.Domain;
using LocalBoard.Controller;
using LocalBoard.Persistence;

namespace LocalBoard.Web
{
	public partial class Post : BoardPage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			int msgId = -1;
			int x,y,z;
			x = y = z = 0;
			try {	msgId = Convert.ToInt32(Request.Params["msgid"]);	} catch (Exception ex) { }
			try {	x = Convert.ToInt32(Request.Params["x"]);	} catch (Exception ex) { }
			try {	y = Convert.ToInt32(Request.Params["y"]);	} catch (Exception ex) { }
			try {	z = Convert.ToInt32(Request.Params["z"]);	} catch (Exception ex) { }
			string body = Request.Params["body"] != null ? Security.GetCleanUserInput(Server.HtmlDecode(Request.Params["body"])) : null;
			int newMsgId = -1;
			if (msgId > 0)
			{
				Message msg = BoardController.GetMessage(msgId);
				msg.X = x;
				msg.Y = y;
				msg.Z = z;
				BoardController.EditMessage(msg, "", body != null ? body : msg.Body);
			}
			else
			{
				Message msg = new Message("", body, this.ClubUser.UserName, this.ClubUser.UserId, this.ClubUser, BoardController.BoardId, ClubController.ClubId, 0);
				msg.X = x;
				msg.Y = y;
				msg.Z = z;
				newMsgId = BoardController.PostMessage(msg);
			}
			Response.ClearContent();
			Response.Write(newMsgId.ToString());
			Response.End();
		}
	}
}